package com.cinco.server.service.impl;

import com.cinco.server.pojo.Department;
import com.cinco.server.mapper.DepartmentMapper;
import com.cinco.server.pojo.Result;
import com.cinco.server.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cinco
 * @since 2022-01-11
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    /**
     * 获取所有部门
     * @return
     */
    @Override
    public List<Department> getAllDepartments() {
        return baseMapper.getAllDepartments(-1);

    }

    /**
     * 添加部门信息
     * @param dept
     * @return
     */
    @Override
    public Result addDepartment(Department dept) {
        dept.setEnabled(true);
        baseMapper.addDepartment(dept);
        //调用完存储过程后返回来的两个结果会直接赋值给dept对象的result和id两列，我们可以直接从对象中拿到输出结果
        if (1 == dept.getResult()){
            return Result.success("添加成功！",dept);
        }
        return Result.error("添加失败！");
    }

    /**
     * 删除部门信息
     * @param id
     * @return
     */
    @Override
    public Result delDepartment(Integer id) {
        Department dep = new Department().setId(id);
        baseMapper.delDepartment(dep);
        if (-2 == dep.getResult()){
            return Result.error("该部门下还有子部门，删除失败！");
        }
        if (-1 == dep.getResult()){
            return Result.error("该部门下还有员工，删除失败！");
        }
        if (1 == dep.getResult()){
            return Result.success("删除成功！");
        }
        return Result.error("删除失败！");
    }
}
